package com.jonluo.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jonluo.exception.RReturnException;
import com.jonluo.model.SysRole;
import com.jonluo.service.SysRoleService;
import com.jonluo.utils.PageInfo;
import com.jonluo.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author jonluo
 * Date 2017/9/15
 */
@RestController
@RequestMapping(value = "/role")
@Api(description = "用户角色服务api")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @ApiOperation(value = "获取用户角色列表", notes = "分页查询用户角色", response = Map.class)
    @ApiImplicitParam(name = "jsonObject", value = "json对象参数 例子{\"page\":1,\"limit\":10,\"params\":" +
            "{\"mobile\":\"123\",\"username\":\"ljw\"} }", required = true, dataType = "JsonObject")
    @RequestMapping(value = {"/getAll"}, method = RequestMethod.POST)
    public ResponseEntity getAll(@RequestBody JSONObject jsonObject) {
        R r = null;
        PageInfo pageInfo = new PageInfo(jsonObject);
        JSONObject json = new JSONObject();
        List<SysRole> list = sysRoleService.queryList(pageInfo);
        Integer total = sysRoleService.queryTotal(pageInfo);
        json.put("total", total);
        json.put("data", list);
        r = R.ok(json);
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "获取用户角色", notes = "根据id查询用户角色", response = Map.class)
    @ApiImplicitParam(name = "id", value = "用户角色id", paramType = "path", required = true, dataType = "String")
    @RequestMapping(value = {"/getById/{id}"}, method = RequestMethod.GET)
    public ResponseEntity getById(@PathVariable String id) {
        R r = null;
        JSONObject json = new JSONObject();
        SysRole sysRole = sysRoleService.query(id);
        json.put("data", sysRole);
        r = R.ok(json);
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "添加用户角色", notes = "添加用户角色信息", response = Map.class)
    @ApiImplicitParam(name = "jsonObject", value = "json对象参数 {对象实体}", required = true, dataType = "JsonObject")
    @RequestMapping(value = {"/add"}, method = RequestMethod.POST)
    public ResponseEntity add(@RequestBody JSONObject jsonObject) {
        R r = null;
        SysRole sysRole = JSON.parseObject(jsonObject.toJSONString(), SysRole.class);
        Integer sum = sysRoleService.save(sysRole);
        if (sum > 0) {
            r = R.ok();
        } else {
            throw new RReturnException("添加用户角色失败");
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "修改用户角色", notes = "根据id修改用户角色信息", response = Map.class)
    @ApiImplicitParam(name = "jsonObject", value = "json对象参数 {userId,username,password,salt,email,mobile,status,deptId,createTime}", required = true, dataType = "JsonObject")
    @RequestMapping(value = {"/update"}, method = RequestMethod.POST)
    public ResponseEntity update(@RequestBody JSONObject jsonObject) {
        R r = null;
        SysRole sysRole = JSON.parseObject(jsonObject.toJSONString(), SysRole.class);
        Integer sum = sysRoleService.update(sysRole);
        if (sum > 0) {
            r = R.ok();
        } else {
            throw new RReturnException("更新用户角色失败");
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "删除用户角色", notes = "批量删除用户角色", response = Map.class)
    @ApiImplicitParam(name = "jsonObject", value = "json对象参数 例子{\"roleIds\":\"id1,id2\"}多个id以逗号隔开", required = true, dataType = "JsonObject")
    @RequestMapping(value = {"/deleteByIds"}, method = RequestMethod.POST)
    public ResponseEntity deleteByIds(@RequestBody JSONObject jsonObject) {
        R r = null;
        String[] ids= jsonObject.getString("roleIds").split(",");
        Integer sum = sysRoleService.deleteBatch(ids);
        if (sum == ids.length) {
            r = R.ok();
        } else {
            throw new RReturnException("批量删除用户角色失败");
        }
        return ResponseEntity.ok(r);
    }


}
